﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Autofac;
using Missbot.AIModule;
using Missbot.Common;
using Missbot.Core;
using Missbot.Core.Base;
using Missbot.Core.Common;
using Missbot.Model.MsgCmds;
using Missbot.Service;
using Missbot.WeiboBot;

namespace Missbot.Boost
{
    /// <summary>
    /// 机器人启动器
    /// </summary>
    public class BotBooster
    {
        #region Svcs

        private static ILogSvc ILogSvc => AutofacSvc.Resolve<ILogSvc>();
        private static AIModuleMgr AIModuleMgr => AutofacSvc.Resolve<AIModuleMgr>();
        private static ChannelMgr ChannelMgr => AutofacSvc.Resolve<ChannelMgr>();
        private static WSMgr WSMgr => AutofacSvc.Resolve<WSMgr>();
        
        #endregion

        private static InstanceConfig InstanceConfig => Configger<AIConfigBase>.AIConfig.Instance;

        /// <summary>
        /// 机器人初始化
        /// </summary>
        /// <param name="extAssemblies">需要额外注册到容器的程序集列表</param>
        public static void InitRobot(IEnumerable<Assembly> extAssemblies = null)
        {
            var assemblies = GetAssemblies();
            if (extAssemblies != null)
            {
                assemblies = assemblies.Concat(extAssemblies).ToArray();
            }

            try
            {
                var builder = new ContainerBuilder();
                builder.Register(_ => new LogSvc()).As<ILogSvc>().SingleInstance();
                AutofacSvc.AllAssemblies = assemblies.ToList();
                AutofacSvc.RegisterAutofac(builder);

                if (InstanceConfig.EnableModule)
                {
                    AutofacSvc.RegisterDataRefresher();
                    AIModuleMgr.InitModules();
                }

                ChannelMgr.Init(AIModuleMgr.OnInbox);

                if (InstanceConfig.EnableSchedule)
                {
                    AIModuleMgr.InitSchedules();
                }

                if (InstanceConfig.EnableListening)
                {
                    Task.Run(async () => await StartListen());
                }
            }
            catch (Exception e)
            {
                ILogSvc.LogError(e);
            }
        }

        /// <summary>
        /// 开始监听websocket
        /// </summary>
        /// <returns></returns>
        public static async Task StartListen()
        {
            try
            {
                await WSMgr.InitAsync(ChannelMgr.OnMessageReceived);

                AIAnalyzer.Sys_StartTime = DateTime.Now;
            }
            catch (Exception e)
            {
                ILogSvc.LogError(e);
            }
        }

        /// <summary>
        /// 获取默认的程序集列表
        /// </summary>
        /// <returns></returns>
        private static Assembly[] GetAssemblies()
        {
            return new[]
            {
                Assembly.GetAssembly(typeof(ISingleton)), // Missbot.Common
                Assembly.GetAssembly(typeof(DbBaseEntity)), // Missbot.Service
                Assembly.GetAssembly(typeof(AIModuleMgr)), // Missbot.AIModule
                Assembly.GetAssembly(typeof(IMsgChannel)), // Missbot.Core
                Assembly.GetAssembly(typeof(IMsgCommand)), // Missbot.Model
                Assembly.GetAssembly(typeof(WeiboRequestor)), // Missbot.WeiboBot
                Assembly.GetAssembly(typeof(MisskeyClient.MisskeyClient)) // Missbot.MisskeyClient
            };
        }
    }
}